﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccessLayer.Models;
using PresentationLayerMVC.Models;

namespace PresentationLayerMVC.Controllers
{
    public class NewAppController : BaseController
    {
        //
        // GET: /NewApp/Create
        public ActionResult AddApplicant()
        {
            initDisplay();
            return View();
        }

        //
        // POST: /NewApp/AddApplicant
        [HttpPost]
        public ActionResult AddApplicant(FormCollection collection)
        {
            IValueProvider info = collection.ToValueProvider();
            PutAppModelInSession(INFO_CUST, info);

            return Next(Constants.ACT_ADDAPPLICANT);
        }

        //
        // POST: /NewApp/AddFacility
        [HttpPost]
        public ActionResult AddFacility()
        {
            initDisplay();
            return View();
        }

        //
        // POST: /NewApp/AddFacility
        [HttpPost]
        public ActionResult AddFacility(FormCollection collection)
        {
            IValueProvider info = collection.ToValueProvider();
            PutAppModelInSession(INFO_CUST, info);

            return Next(Constants.ACT_CREATE_NEWAPP);
        }

        public ActionResult Summary()
        {
            using (LOSEntities ctx = new LOSEntities())
            {
                AppModel appInfo = (AppModel)Session["ApplicationInfo"];

                Applicant cust = new Applicant();
                TryUpdateModel(cust, appInfo.custInfo);
                cust.Version = DateTime.Now;
                cust.Address.Version = DateTime.Now;

                Facility fact = new FacilityFactory().create(GetProductFromSession());
                if (fact is FacilityHousingLoan)
                {
                    ViewData["LoanType"] = "Housing Loan";
                }
                else if (fact is FacilityOverDraft)
                {
                    ViewData["LoanType"] = "Overdraft";
                }
                else if (fact is FacilityTermLoan)
                {
                    ViewData["LoanType"] = "Term Loan";
                }
                TryUpdateModel(fact, appInfo.facilityInfo[0]);
                fact.Version = DateTime.Now;

                int userId = GetUserIdFromSession();
                User user = ctx.Users.Single(u => u.UserId == userId);
                Application app = new Application();
                app.Customer = cust;
                app.CreatorId = user.UserId;
                app.Role = user.Role;
                app.Facilities.Add(fact);
                app.CreateDate = DateTime.Now;
                app.Version = DateTime.Now;

                Security security = SecurityFactory.getInstance().create(GetSecurityTypeFromSession());
                if (security is Mortgage)
                {
                    Mortgage sec = (Mortgage)security;
                    TryUpdateModel(sec, appInfo.securityInfo[0]);
                    sec.Mortgagors = Mortgagor.CreateMortgagor(cust.Name, cust.Gender, cust.DOB,
                                                                cust.MobilePhone,
                                                                cust.Email,
                                                                0,
                                                                cust.TotalIncome,
                                                                DateTime.Now,
                                                                0,
                                                                cust.IDNumber,
                                                                cust.IDTypeCode);

                    sec.Mortgagors.Address = cust.Address;
                    sec.Properties.Address.Version = DateTime.Now;
                    app.Securities.Add(sec);
                }
                else if (security is PersonalGuarantee)
                {
                    PersonalGuarantee sec = (PersonalGuarantee)security;
                    TryUpdateModel(sec, appInfo.securityInfo[0]);
                    sec.Guarantors.Version = DateTime.Now;
                    sec.Guarantors.Address.Version = DateTime.Now;
                    app.Securities.Add(sec);
                }
                else
                {
                    FixedDeposit sec = (FixedDeposit)security;
                    TryUpdateModel(sec, appInfo.securityInfo[0]);
                    app.Securities.Add(sec);
                }

                try
                {
                    ctx.Applications.AddObject(app);
                    ctx.SaveChanges();

                    ClearAppInfoInSession();

                    return View(app);
                }
                catch
                { }
                return Next(Constants.ACT_LOGIN);
            }
        }
    }
}
